<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Default Kohana controller. This controller should NOT be used in production.
 * It is for demonstration purposes only!
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Find_Hongshaw_Controller extends Template_Controller {

	// Disable this controller when Kohana is set to production mode.
	// See http://docs.kohanaphp.com/installation/deployment for more details.
	const ALLOW_PRODUCTION = FALSE;

	// Set the name of the template to use
	public $template = 'layouts/template_hongshaw_detail';

	public function index(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_bkkzone');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		$this->template->title = 'ค้นหาห้องเช่า';
	}
	
 	public function find_bkkzone(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_bkkzone');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		$this->template->title = 'ค้นหาห้องเช่า';
	}
 	public function find_university(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_university');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		$this->template->title = 'ค้นหาห้องเช่า';
	}
 	public function find_province(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_province');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		$this->template->title = 'ค้นหาห้องเช่า';
	}
	
	public function listBangkokZone(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_bkkzone');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		if ($_POST){
			$bkkzone_id=$this->input->post("bkkzone_id");
			$hongshaw_min_price =$this->input->post("price");
			$hongshaw_max_price =$this->input->post("price");
			$category_id=$this->input->post("category_id");
			$total_items =0;
			if ($bkkzone_id==0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 1
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id !=0");
			}else if($bkkzone_id!=0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 2
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id =".$bkkzone_id."");
			}else if($bkkzone_id==0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 3
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id !=0",
								"hongshaw_min_price >=".$hongshaw_min_price."");
			}else if($bkkzone_id==0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 4
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id !=0",
								"hongshaw_min_price !=0",
								"category_id =".$category_id."");
			}else if($bkkzone_id!=0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 5
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id =".$bkkzone_id."",
								"hongshaw_min_price >=".$hongshaw_min_price."");
			}else if($bkkzone_id==0 && $hongshaw_min_price!=0 && $category_id!=0){
				// Step 6
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id !=0",
								"hongshaw_min_price >=".$hongshaw_min_price."",
								"category_id =".$category_id."");
			}else if($bkkzone_id!=0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 7
				$total_items = Database::instance()->count_records("hongshaws",
								"bkkzone_id =".$bkkzone_id."",
								"hongshaw_min_price !=0",
								"category_id =".$category_id."");
			}
		    $this->template->content->total_items=$total_items;
		    //Pagination
		    $pagination = new Pagination(array(
		        'base_url'    => 'find_hongshaw/find_bkkzone/',
		        'uri_segment' => 'search', 
		        'total_items' => $total_items,
		    	'items_per_page' => 10
		    ));
		    
			if ($bkkzone_id==0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 1
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id !=0 ",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($bkkzone_id!=0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 2
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id =".$bkkzone_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($bkkzone_id==0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 3
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id !=0 and hongshaw_min_price >=".$hongshaw_min_price."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($bkkzone_id==0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 4
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id !=0 and hongshaw_min_price !=0 and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($bkkzone_id!=0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 5
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id =".$bkkzone_id." and hongshaw_min_price >=".$hongshaw_min_price."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
				
			}else if($bkkzone_id==0 && $hongshaw_min_price!=0 && $category_id!=0){
				// Step 6
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id !=0 and hongshaw_min_price >=".$hongshaw_min_price." and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($bkkzone_id!=0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 7
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"bkkzone_id =".$bkkzone_id." and hongshaw_min_price !=0 and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}
		    $this->template->content->hongshaws =$hongshaws; 										
		    $this->template->content->pagination=$pagination;	
		}
	    
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	
		
	}
	
	public function listUniversity(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_university');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		if ($_POST){

			$university_id=$this->input->post("university_id");
			$hongshaw_min_price =$this->input->post("price");
			$hongshaw_max_price =$this->input->post("price");
			$category_id=$this->input->post("category_id");
			$total_items =0;
			if ($university_id==0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 1
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id !=0");
			}else if($university_id!=0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 2
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id =".$university_id."");
			}else if($university_id==0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 3
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id !=0",
								"hongshaw_min_price >=".$hongshaw_min_price."");
			}else if($university_id==0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 4
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id !=0",
								"hongshaw_min_price !=0",
								"category_id =".$category_id."");
			}else if($university_id!=0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 5
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id =".$university_id."",
								"hongshaw_min_price >=".$hongshaw_min_price."");
			}else if($university_id==0 && $hongshaw_min_price!=0 && $category_id!=0){
				// Step 6
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id !=0",
								"hongshaw_min_price >=".$hongshaw_min_price."",
								"category_id =".$category_id."");
			}else if($university_id!=0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 7
				$total_items = Database::instance()->count_records("hongshaws",
								"university_id =".$university_id."",
								"hongshaw_min_price !=0",
								"category_id =".$category_id."");
			}
		    $this->template->content->total_items=$total_items;
		    //Pagination
		    $pagination = new Pagination(array(
		        'base_url'    => 'find_hongshaw/find_bkkzone/',
		        'uri_segment' => 'search', 
		        'total_items' => $total_items,
		    	'items_per_page' => 10
		    ));
		    
			if ($university_id==0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 1
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id !=0 ",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($university_id!=0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 2
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id =".$university_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($university_id==0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 3
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id !=0 and hongshaw_min_price >=".$hongshaw_min_price."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($university_id==0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 4
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id !=0 and hongshaw_min_price !=0 and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($university_id!=0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 5
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id =".$university_id." and hongshaw_min_price >=".$hongshaw_min_price."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
				
			}else if($university_id==0 && $hongshaw_min_price!=0 && $category_id!=0){
				// Step 6
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id !=0 and hongshaw_min_price >=".$hongshaw_min_price." and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($university_id!=0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 7
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"university_id =".$university_id." and hongshaw_min_price !=0 and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}
		    $this->template->content->hongshaws =$hongshaws; 										
		    $this->template->content->pagination=$pagination;	
		}
	    
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	
		
	}
	public function listProvince(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('form_province');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->total_items=0;
		if ($_POST){

			$province_id=$this->input->post("province_id");
			$hongshaw_min_price =$this->input->post("price");
			$hongshaw_max_price =$this->input->post("price");
			$category_id=$this->input->post("category_id");
			$total_items =0;
			if ($province_id==0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 1
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id !=0");
				echo ">> province_id > ".$province_id."| count > ".$total_items;
			}else if($province_id!=0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 2
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id =".$province_id."");
			}else if($province_id==0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 3
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id !=0",
								"hongshaw_min_price >=".$hongshaw_min_price."");
			}else if($province_id==0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 4
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id !=0",
								"hongshaw_min_price !=0",
								"category_id =".$category_id."");
			}else if($province_id!=0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 5
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id =".$province_id."",
								"hongshaw_min_price >=".$hongshaw_min_price."");
			}else if($province_id==0 && $hongshaw_min_price!=0 && $category_id!=0){
				// Step 6
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id !=0",
								"hongshaw_min_price >=".$hongshaw_min_price."",
								"category_id =".$category_id."");
			}else if($province_id!=0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 7
				$total_items = Database::instance()->count_records("hongshaws",
								"province_id =".$province_id."",
								"hongshaw_min_price !=0",
								"category_id =".$category_id."");
			}
		    $this->template->content->total_items=$total_items;
		    //Pagination
		    $pagination = new Pagination(array(
		        'base_url'    => 'find_hongshaw/find_bkkzone/',
		        'uri_segment' => 'search', 
		        'total_items' => $total_items,
		    	'items_per_page' => 10
		    ));
		    
			if ($province_id==0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 1
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id !=0 ",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($province_id!=0 && $hongshaw_min_price==0 && $category_id==0){
				// Step 2
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id =".$province_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
		    							echo $hongshaws;
			}else if($province_id==0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 3
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id !=0 and hongshaw_min_price >=".$hongshaw_min_price."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($province_id==0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 4
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id !=0 and hongshaw_min_price !=0 and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($province_id!=0 && $hongshaw_min_price!=0 && $category_id==0){
				// Step 5
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id =".$province_id." and hongshaw_min_price >=".$hongshaw_min_price."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
				
			}else if($province_id==0 && $hongshaw_min_price!=0 && $category_id!=0){
				// Step 6
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id !=0 and hongshaw_min_price >=".$hongshaw_min_price." and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}else if($province_id!=0 && $hongshaw_min_price==0 && $category_id!=0){
				// Step 7
				$hongshaws = Database::instance()->getwhere("hongshaws",
		    							"province_id =".$province_id." and hongshaw_min_price !=0 and category_id =".$category_id."",
		    							$pagination->items_per_page,$pagination->sql_offset)->as_array();
			}
		    $this->template->content->hongshaws =$hongshaws; 										
		    $this->template->content->pagination=$pagination;	
		}
	    
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	
		
	}
	public function search($keyword){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('main');
		//sakorn.s
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->keyword=$keyword;
		$total_items = Database::instance()->count_records("hongshaws","hongshaw_name like '%".$keyword."%'");
	    $this->template->content->product_count=$total_items;
	    $this->template->content->keyword=$keyword;
	    
	    //Pagination
	    $pagination = new Pagination(array(
	        'base_url'    => 'find_hongshaw/search/',
	        'uri_segment' => 'search', 
	        'total_items' => $total_items,
	    	'items_per_page' => 10
	    ));
	    
	    $hongshaws = Database::instance()->getwhere("hongshaws",
	    									"hongshaw_name like '%".$keyword."%'",
	    									$pagination->items_per_page,$pagination->sql_offset)->as_array();
	    $this->template->content->hongshaws =$hongshaws; 
	    											
	    $this->template->content->pagination=$pagination;	
	    $this->template->title = '';
	    
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;

		// By defining a __call method, all pages routed to this controller
		// that result in 404 errors will be handled by this method, instead of
		// being displayed as "Page Not Found" errors.
		echo 'This text is generated by __call. If you expected the index page, you need to use: welcome/index/'.substr(Router::$current_uri, 8);
	}

} // End Welcome Controller